#!/bin/bash

# 设置颜色输出
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

echo -e "${GREEN}=== 彻底修复BLESDK蓝牙权限问题 ===${NC}"

# 导航到项目根目录
echo -e "${GREEN}导航到项目根目录...${NC}"
cd ~/Desktop/BLESDK

# 查找所有Info.plist文件
echo -e "${YELLOW}查找所有Info.plist文件...${NC}"
INFO_PLISTS=$(find . -name "Info.plist")
echo -e "${GREEN}找到以下Info.plist文件:${NC}"
echo "$INFO_PLISTS"

# 为所有Info.plist添加蓝牙权限
echo -e "${YELLOW}正在为所有Info.plist添加蓝牙权限...${NC}"
for plist in $INFO_PLISTS; do
    echo -e "${YELLOW}处理: $plist${NC}"
    
    # 创建备份
    cp "$plist" "${plist}.backup"
    
    # 使用plutil检查并添加权限 (比sed更可靠)
    if ! grep -q "NSBluetoothAlwaysUsageDescription" "$plist"; then
        echo -e "${RED}添加NSBluetoothAlwaysUsageDescription到$plist${NC}"
        /usr/libexec/PlistBuddy -c "Add :NSBluetoothAlwaysUsageDescription string '此应用需要访问蓝牙来扫描和连接设备'" "$plist"
    else
        echo -e "${GREEN}$plist已包含NSBluetoothAlwaysUsageDescription√${NC}"
    fi
    
    if ! grep -q "NSBluetoothPeripheralUsageDescription" "$plist"; then
        echo -e "${RED}添加NSBluetoothPeripheralUsageDescription到$plist${NC}"
        /usr/libexec/PlistBuddy -c "Add :NSBluetoothPeripheralUsageDescription string '此应用需要访问蓝牙来扫描和连接设备'" "$plist"
    else
        echo -e "${GREEN}$plist已包含NSBluetoothPeripheralUsageDescription√${NC}"
    fi
done

# 检查项目设置
echo -e "${YELLOW}修复项目设置...${NC}"
cd Example

# 重建pod
echo -e "${YELLOW}重建pod...${NC}"
pod deintegrate
pod install

# 为确保Info.plist设置正确，创建一个新的Info.plist文件
echo -e "${YELLOW}创建一个新的更完整的Info.plist文件...${NC}"
cat > BLESDK/NewInfo.plist << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>zh_CN</string>
    <key>CFBundleDisplayName</key>
    <string>BLESDK示例</string>
    <key>CFBundleExecutable</key>
    <string>\$(EXECUTABLE_NAME)</string>
    <key>CFBundleIdentifier</key>
    <string>\$(PRODUCT_BUNDLE_IDENTIFIER)</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>\$(PRODUCT_NAME)</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>CFBundleVersion</key>
    <string>1</string>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>NSBluetoothAlwaysUsageDescription</key>
    <string>此应用需要使用蓝牙来扫描和连接设备</string>
    <key>NSBluetoothPeripheralUsageDescription</key>
    <string>此应用需要使用蓝牙来扫描和连接设备</string>
    <key>UILaunchStoryboardName</key>
    <string>LaunchScreen</string>
    <key>UIRequiredDeviceCapabilities</key>
    <array>
        <string>armv7</string>
    </array>
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
    </array>
</dict>
</plist>
EOF

echo -e "${YELLOW}备份原Info.plist并使用新文件...${NC}"
mv BLESDK/Info.plist BLESDK/Info.plist.old
mv BLESDK/NewInfo.plist BLESDK/Info.plist

# 清理缓存
echo -e "${YELLOW}清理缓存...${NC}"
rm -rf ~/Library/Developer/Xcode/DerivedData/BLESDK*

# 添加一个构建脚本确保权限被包含
echo -e "${YELLOW}创建一个构建脚本来确保权限被正确包含...${NC}"
mkdir -p BuildScripts
cat > BuildScripts/ensure_permissions.sh << EOF
#!/bin/bash
# 此脚本在编译前确保权限被正确包含

if [ -f "\${SRCROOT}/\${INFOPLIST_FILE}" ]; then
    echo "确保蓝牙权限被添加到Info.plist"
    
    /usr/libexec/PlistBuddy -c "Delete :NSBluetoothAlwaysUsageDescription" "\${SRCROOT}/\${INFOPLIST_FILE}" 2>/dev/null || true
    /usr/libexec/PlistBuddy -c "Add :NSBluetoothAlwaysUsageDescription string '此应用需要使用蓝牙来扫描和连接设备'" "\${SRCROOT}/\${INFOPLIST_FILE}"
    
    /usr/libexec/PlistBuddy -c "Delete :NSBluetoothPeripheralUsageDescription" "\${SRCROOT}/\${INFOPLIST_FILE}" 2>/dev/null || true
    /usr/libexec/PlistBuddy -c "Add :NSBluetoothPeripheralUsageDescription string '此应用需要使用蓝牙来扫描和连接设备'" "\${SRCROOT}/\${INFOPLIST_FILE}"
    
    echo "Info.plist更新完成"
else
    echo "错误: 找不到Info.plist文件: \${SRCROOT}/\${INFOPLIST_FILE}"
fi
EOF

chmod +x BuildScripts/ensure_permissions.sh

echo -e "${GREEN}修复完成!${NC}"
echo -e "${YELLOW}请在Xcode中执行以下操作:${NC}"
echo -e "${YELLOW}1. 在Xcode中打开BLESDK.xcworkspace${NC}"
echo -e "${YELLOW}2. 选择BLESDK_Example目标${NC}"
echo -e "${YELLOW}3. 点击'Build Phases'选项卡${NC}"
echo -e "${YELLOW}4. 点击左上角的'+'按钮，选择'New Run Script Phase'${NC}"
echo -e "${YELLOW}5. 将以下内容粘贴到脚本框中:${NC}"
echo -e "${YELLOW}   \${SRCROOT}/BuildScripts/ensure_permissions.sh${NC}"
echo -e "${YELLOW}6. 拖动这个新的Run Script阶段到'Copy Bundle Resources'之前${NC}"
echo -e "${YELLOW}7. 选择菜单 Product > Clean Build Folder (Shift+Command+K)${NC}"
echo -e "${YELLOW}8. 重新编译并运行项目${NC}"

# 自动打开项目
echo -e "${YELLOW}正在打开项目...${NC}"
open BLESDK.xcworkspace 